package br.ufrj.dcc.smarthotel;

import java.util.LinkedList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

public class DirectionsActivity extends Activity {
	
	//private final String routeUrl = "http://maps.google.es/maps/api/directions/json";
	//private final String mapUrl = "http://maps.google.com/maps/api/staticmap";
	//private final String HotelLocation = "-22.853201,-43.230822";
	private JSONObject route;
	
	/***
     * Metodo chamado na criacao da Activity
     * @param savedInstanceState - Parametros para a inicializacao da Activity
     */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	//Chama o OnCreate da Classe Activity
        super.onCreate(savedInstanceState);
        
        //Recupera o bundle para pegar o parametro passado pela Activity
        Bundle bundle = this.getIntent().getExtras();
        
        setContentView(R.layout.directions);
        
        //Pega o parametro passado pela Activity
        List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
        params.add(new BasicNameValuePair("origin", Constants.HOTEL_COORDINATES));
        params.add(new BasicNameValuePair("destination", Double.toString(bundle.getDouble("latitude")) + "," + Double.toString(bundle.getDouble("longitude"))));
        params.add(new BasicNameValuePair("sensor", "false"));
        
        this.route = Util.getInstance(this.getApplicationContext()).serverHttpGetObject(Constants.GOOGLE_DIRECTIONS_URL, params);
        
        params.clear();
        
        try {
			JSONObject leg = this.route.getJSONArray("routes").getJSONObject(0).getJSONArray("legs").getJSONObject(0);
			
			params.add(new BasicNameValuePair("markers", "color:red|color:red|label:A|" + Util.getInstance(this.getApplicationContext()).getFormatedLocation(leg.getJSONObject("start_location"))));
			params.add(new BasicNameValuePair("markers", "color:red|color:red|label:B|" + Util.getInstance(this.getApplicationContext()).getFormatedLocation(leg.getJSONObject("end_location"))));
			
			String pathString = "";
			JSONArray steps = leg.getJSONArray("steps");
			for (int i = 0; i < steps.length(); i++) {
				JSONObject step = steps.getJSONObject(i);
				pathString +=  Util.getInstance(this.getApplicationContext()).getFormatedLocation(step.getJSONObject("start_location")) + "|";
			}
			pathString += Util.getInstance(this.getApplicationContext()).getFormatedLocation(leg.getJSONObject("end_location"));
			
			params.add(new BasicNameValuePair("path", "color:0x0000ff|weight:5|" + pathString));
			params.add(new BasicNameValuePair("size", "512x512"));
			params.add(new BasicNameValuePair("maptype", "roadmap"));
			params.add(new BasicNameValuePair("sensor", "false"));
			
			ImageView imgMap = (ImageView) findViewById(R.id.imgMap);
			imgMap.setImageBitmap(Util.getInstance(this.getApplicationContext()).downloadImage(Util.getInstance(this.getApplicationContext()).urlBuilder(Constants.GOOGLE_STATIC_MAP_URL, params)));
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        //&size=512x512&maptype=roadmap&sensor=false
        
		//path=color:0x0000ff|weight:5|40.737102,-73.990318|40.749825,-73.987963|40.752946,-73.987384|40.755823,-73.986397
        
        //this.setListAdapter(new ListAdapterImageTextMenu(this, sightList));
    }

}
